<?php

/**
* @current scorerule.php
* @author   CaoQiCheng <caoqicheng@haibao.com>
* @date 2016年8月22日 上午10:11:24
*/

namespace haibao\user\common;

use \haibao\app\business\BusinessException;
use \haibao\user\cache\UserIntegral as UserIntegralCache;
use \haibao\user\data\UserIntegral as UserIntegralData;
use \haibao\user\model\data\UserIntegral as UserIntegralModel;
use \haibao\user\model\data\UserIntegralHistory as UserIntegralHistoryModel;
use \haibao\user\model\filter\UserIntegral as UserIntegralFilter;
use haibao\frame\data\query\Condition;

class ScoreRule extends \haibao\user\business\Base{
	
	const TYPE_LOGIN = 1;//登陆
	const TYPE_REGISTER = 2;//注册
	const TYPE_COMMENT = 3;//评论
	const TYPE_PARISE = 4;//点赞
	const TYPE_VOTE = 5;//投票
	const TYPE_MARK = 6;//收藏
	const TYPE_ARTICLE_VIEW = 7;//看文章领积分
	const TYPE_SHARE = 8;//分享
	const TYPE_PUBLISH_ARTICLE = 9;//发布博文
	const TYPE_PUBLISH_TOPIC = 10;//发布帖子
	const TYPE_DEL_COMMENT = 11;//删除评论
	const TYPE_DEL_ARTICLE = 12;//删除博文
	const TYPE_DEL_TOPIC = 13;//删除帖子
	
	const SCORE_LOGIN = 100;//登陆分数
	const SCORE_REGISTER = 1000;//注册分数
	const SCORE_COMMENT = 100;//评论分数
	const SCORE_PARISE = 10;//点赞分数
	const SCORE_VOTE = 50;//投票分数
	const SCORE_MARK = 10;//收藏分数
	const SCORE_ARTICLE_VIEW = 1;//看文章领积分分数
	const SCORE_SHARE = 500;//分享分数
	const SCORE_PUBLISH_ARTICLE = 1000;//发布博文分数
	const SCORE_PUBLISH_TOPIC = 1000;//发布帖子
	const SCORE_DEL_COMMENT = -100;//删除评论
	const SCORE_DEL_ARTICLE = -1000;//删除博文
	const SCORE_DEL_TOPIC = -1000;//删除帖子
	
	
	/**
	 * 用户积分所有的操作
	 * 参数 userId   积分的种类默认登陆scoreType 是否是删除默认 0不是删除 1是删除
	 * 
	 * 加上对应的条件信息detailInfo,传入的是对应操作需要的实体
	 *
	 */
	public static function operateScore($userId,$scoreType = self::TYPE_LOGIN,$detailInfo = null){
		if (!$userId){
			return 0;
		}
		$userIntegral = 0;
		self::isExistIntegral($userId);
		
		switch ($scoreType){
			case self::TYPE_LOGIN: 
				$userIntegral = self::scoreLogin($userId);break;
			case self::TYPE_REGISTER: 
				$userIntegral = self::scoreRegister($userId);break;
			case self::TYPE_COMMENT: 
				$userIntegral = self::scoreComment($userId,$detailInfo);break;
			case self::TYPE_PARISE:
				$userIntegral = self::scoreParise($userId);break;
			case self::TYPE_VOTE: 
				$userIntegral = self::scoreVote($userId);break;
			case self::TYPE_MARK: 
				$userIntegral = self::scoreMark($userId);break;
			case self::TYPE_ARTICLE_VIEW: 
				$userIntegral = self::scoreArticleView($userId);break;
			case self::TYPE_SHARE: 
				$userIntegral = self::scoreShare($userId);break;
			case self::TYPE_PUBLISH_ARTICLE: 
				$userIntegral = self::scoreArticle($userId);break;
			case self::TYPE_PUBLISH_TOPIC: 
				$userIntegral = self::scoreTopic($userId);break;
			case self::TYPE_DEL_COMMENT:
				$userIntegral = self::scoreDelComment($userId, $detailInfo);break;
			case self::TYPE_DEL_ARTICLE:
				$userIntegral = self::scoreDelArticle($userId);break;
			case self::TYPE_DEL_TOPIC:
				$userIntegral = self::scoreDelTopic($userId);break;
			default: break;
		}
		
		return $userIntegral;
	}
	
	/**
	 * 获取redis缓存数据
	 */
	public static function getUserIntergral($userId){
		$userCache = new UserIntegralCache();
		$userIntegralCache = $userCache->getUserIntegral($userId);
		if(!$userIntegralCache){
			$userCacheData = array(
					self::TYPE_LOGIN => array('ContinuityLogin'=>0),
					self::TYPE_PARISE => array('ContinuityParise'=>0),
					self::TYPE_VOTE => array('ContinuityVote'=>0),
					self::TYPE_MARK => array('ContinuityMark'=>0),
					self::TYPE_ARTICLE_VIEW => array('ContinuityArticleView'=>0),
			);
			$userCache->setUserIntegral($userId,$userCacheData);
			$userIntegralCache = $userCache->getUserIntegral($userId);
		}
		return json_decode($userIntegralCache,true);
		
	}
	
	/**
	 * 判断是否存在过这个用户的积分数据 不存在先添加数据
	 */
	public static function isExistIntegral($userId){
		$userIntegralInfo = null;
		$userIntegralFilter = new UserIntegralFilter();
		$userIntegralFilter->select(array(
				$userIntegralFilter::CONDITION_FIELD_NAME_USER_ID,
				$userIntegralFilter::CONDITION_FIELD_NAME_ID,
		));
		$userIntegralFilter->where($userIntegralFilter::CONDITION_FIELD_NAME_USER_ID, Condition::CONDITION_EQUAL, (int)$userId);
		
		$userIntegralData = new UserIntegralData();
		$userIntegralInfo = $userIntegralData->getOne($userIntegralFilter);
		
		if(!$userIntegralInfo){
			$userIntegralModel = new UserIntegralModel();
			$userIntegralModel->UserId = $userId;
			$userIntegralModel->CreateTime = new \DateTime();
			$userIntegralModel->Integrals = 0;
			$userIntegralData->add($userIntegralModel);
		}
		
	}

	/**
	 * 登陆时积分计算  
	 * 未缓存时增加积分，缓存没有登陆时间增加积分，缓存的上次登陆时间在今天之前的增加积分
	 */
	public static function scoreLogin($userId){
		$userIntegral = 0;
		$userCacheData = self::getUserIntergral($userId);
			
		if($userCacheData[self::TYPE_LOGIN]['ContinuityLogin'] == 0){
			$isSetCache = true;
			$userCacheData[self::TYPE_LOGIN]['ContinuityLogin'] = $userCacheData[self::TYPE_LOGIN]['ContinuityLogin'] + 1;
			
			$userCache = new UserIntegralCache();
			$userCache->setUserIntegral($userId,$userCacheData);
			$userIntegral = self::SCORE_LOGIN;
			
			$userIntegralData = new UserIntegralData();
			$userIntegralData->addUserScore($userId,$userIntegral);
			self::saveIntegralHistory($userId,self::TYPE_LOGIN,self::SCORE_LOGIN);
		}
		
		return $userIntegral;
	}
	
	/**
	 * 注册赠送积分
	 * 注册成功后调用
	 */
	public static function scoreRegister($userId){
		$userIntegralData = new UserIntegralData();
		$result = $userIntegralData->addUserScore($userId,self::SCORE_REGISTER);
		self::saveIntegralHistory($userId,self::TYPE_REGISTER,self::SCORE_REGISTER);
		return $result ;
	}
	/**
	 * 评论增加积分
	 */
	public static function scoreComment($userId,$detailInfo){
		
		$result = 0;
		if(isset($detailInfo->IsBack) && $detailInfo->IsBack == \haibao\comment\model\data\Comments::BACK_NO){
			$userIntegralData = new UserIntegralData();

			$content = strip_tags($detailInfo->MyComment,'<img>');
			$content = preg_replace('/\<img[^<]*\>/','',$content);
			$content = str_replace('&nbsp','',$content);
		
			if(mb_strlen($content,'UTF8') >= 10){
				$result =  $userIntegralData->addUserScore($userId,self::SCORE_COMMENT);
				self::saveIntegralHistory($userId,self::TYPE_COMMENT,self::SCORE_COMMENT);
			}
			return $content;
		}
		
		return $result ;
	}
	
	/**
	 * 点赞增加积分,未保存从第一天到今天的总的点赞次数，只保存每天点赞次数
	 */
	public static function scoreParise($userId){
		$userIntegral = 0;
		$userCacheData = self::getUserIntergral($userId);
			
		if($userCacheData[self::TYPE_PARISE]['ContinuityParise'] < 20){
			$isSetCache = true;
			$userCacheData[self::TYPE_PARISE]['ContinuityParise'] = $userCacheData[self::TYPE_PARISE]['ContinuityParise'] + 1;
			
			$userCache = new UserIntegralCache();
			$userCache->setUserIntegral($userId,$userCacheData);
			$userIntegral = self::SCORE_PARISE;
			
			$userIntegralData = new UserIntegralData();
			$userIntegralData->addUserScore($userId,$userIntegral);
			self::saveIntegralHistory($userId,self::TYPE_PARISE,self::SCORE_PARISE);
		}
		
		return $userIntegral;
	}
	
	/**
	 * 投票增加积分
	 */
	public static function scoreVote($userId){
		$userIntegral = 0;
		$userCacheData = self::getUserIntergral($userId);
			
		if($userCacheData[self::TYPE_VOTE]['ContinuityVote'] < 4){
			$isSetCache = true;
			$userCacheData[self::TYPE_VOTE]['ContinuityVote'] = $userCacheData[self::TYPE_VOTE]['ContinuityVote'] + 1;
			
			$userCache = new UserIntegralCache();
			$userCache->setUserIntegral($userId,$userCacheData);
			$userIntegral = self::SCORE_VOTE;
		
			$userIntegralData = new UserIntegralData();
			$userIntegralData->addUserScore($userId,$userIntegral);
			self::saveIntegralHistory($userId,self::TYPE_VOTE,self::SCORE_VOTE);
		}
		
		return $userIntegral;
	}
	
	
	/**
	 * 收藏增加积分
	 */
	public static function scoreMark($userId){
		$userIntegral = 0;
		$userCacheData = self::getUserIntergral($userId);
			
		if($userCacheData[self::TYPE_MARK]['ContinuityMark'] < 20){
			$isSetCache = true;
			$userCacheData[self::TYPE_MARK]['ContinuityMark'] = $userCacheData[self::TYPE_MARK]['ContinuityMark'] + 1;
			
			$userCache = new UserIntegralCache();
			$userCache->setUserIntegral($userId,$userCacheData);
			$userIntegral = self::SCORE_MARK;
			$userIntegralData = new UserIntegralData();
			$userIntegralData->addUserScore($userId,$userIntegral);
			self::saveIntegralHistory($userId,self::TYPE_MARK,self::SCORE_MARK);
		}
		
		return $userIntegral;
	}
	
	/**
	 * 浏览文章增加积分
	 */
	public static function scoreArticleView($userId){
		$userIntegral = 0;
		$userCacheData = self::getUserIntergral($userId);
			
		if($userCacheData[self::TYPE_ARTICLE_VIEW]['ContinuityArticleView'] < 4){
			$isSetCache = true;
			$userCacheData[self::TYPE_ARTICLE_VIEW]['ContinuityArticleView'] = $userCacheData[self::TYPE_ARTICLE_VIEW]['ContinuityArticleView'] + 1;
			
			$userCache = new UserIntegralCache();
			$userCache->setUserIntegral($userId,$userCacheData);
			$userIntegral = self::SCORE_ARTICLE_VIEW;
			$userIntegralData = new UserIntegralData();
			$userIntegralData->addUserScore($userId,$userIntegral);
			self::saveIntegralHistory($userId,self::TYPE_ARTICLE_VIEW,self::SCORE_ARTICLE_VIEW);
		}
		
		return $userIntegral;
	}
	
	
	/**
	 * 分享
	 */
	public static function scoreShare($userId){
		$userIntegralData = new UserIntegralData();
		$result = $userIntegralData->addUserScore($userId,self::SCORE_SHARE);
		self::saveIntegralHistory($userId,self::TYPE_SHARE,self::SCORE_SHARE);
		return $result ;
	}
	
	/**
	 * 发布博文
	 */
	public static function scoreArticle($userId){
		$userIntegralData = new UserIntegralData();
		$result = $userIntegralData->addUserScore($userId,self::SCORE_PUBLISH_ARTICLE);
		self::saveIntegralHistory($userId,self::TYPE_PUBLISH_ARTICLE,self::SCORE_PUBLISH_ARTICLE);
		return $result ;
	}
	
	/**
	 * 发布帖子
	 */
	public static function scoreTopic($userId){
		$userIntegralData = new UserIntegralData();
		$result = $userIntegralData->addUserScore($userId,self::SCORE_PUBLISH_TOPIC);
		self::saveIntegralHistory($userId,self::TYPE_PUBLISH_TOPIC,self::SCORE_PUBLISH_TOPIC);
		return $result;
	}
	
	/**
	 * 删除评论
	 */
	public static function scoreDelComment($userId,$detailInfo){
		
		$result = 0;
		if(isset($detailInfo->IsBack) && $detailInfo->IsBack == \haibao\comment\model\data\Comments::BACK_NO){
			$userIntegralData = new UserIntegralData();
		
			$content = strip_tags($detailInfo->MyComment,'<img>');
			$content = preg_replace('/\<img[^<]*\>/','',$content);
			$content = str_replace('&nbsp','',$content);
		
			if(mb_strlen($content,'UTF8') >= 10){
				$result =  $userIntegralData->addUserScore($userId,self::SCORE_DEL_COMMENT);
				self::saveIntegralHistory($userId,self::TYPE_DEL_COMMENT,self::SCORE_DEL_COMMENT);
			}
		}
		
		return $result ;
	}
	
	/**
	 * 删除博文
	 */
	public static function scoreDelArticle($userId){
		$userIntegralData = new UserIntegralData();
		$result = $userIntegralData->addUserScore($userId,self::SCORE_DEL_ARTICLE);
		self::saveIntegralHistory($userId,self::TYPE_DEL_ARTICLE,self::SCORE_DEL_ARTICLE);
		return $result;
	}
	
	/**
	 * 删除帖子
	 */
	public static function scoreDelTopic($userId){
		$userIntegralData = new UserIntegralData();
		$result = $userIntegralData->addUserScore($userId,self::SCORE_DEL_TOPIC);
		self::saveIntegralHistory($userId,self::TYPE_DEL_TOPIC,self::SCORE_DEL_TOPIC);
		return $result;
	}
	
	/**
	 * 每次对积分的操作进行保存
	 */
	public static function saveIntegralHistory($userId,$integralType,$integralVal){
		$userIntegralHistoryModel = new UserIntegralHistoryModel();
		$userIntegralHistoryModel->IntegralType = $integralType;
		$userIntegralHistoryModel->IntegralVal = $integralVal;
		$userIntegralHistoryModel->UserId = $userId;
		$userIntegralHistoryModel->CreateTime = new \DateTime();
		$userIntegralData = new UserIntegralData();
		$userIntegralData->add($userIntegralHistoryModel);
	}
	
}

